﻿layui.config({
    base: "Scripts/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
		pageData = { "PageSize": 5, "PageCount": 0, "PageNumber": 0, "RowCount": 0 };

    //表格数据和分页
    function getList(that) {
        $.ajax({
            type: "POST",
            url: "/TransferBoss/TransferB",
            data: that,
            dataType: "json",
            success: function (data) {
                var result = JSON.parse(data.Data);
                var usersData = result.DataList;

                pageData.PageCount = result.PageCount;
                pageData.PageNumber = result.PageNumber;
                pageData.PageSize = result.PageSize;
                pageData.RowCount = result.RowCount;
                //执行加载数据的方法
                renderDate(usersData);
                showPage(pageData);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert(textStatus);
            }
        });



        var active = {
            setTop: function (Aid) {
                var that = this;
                $.ajax({
                    type: "POST",
                    url: "/TransferBoss/TransferBupd?id=" + Aid + "",
                    success: function (data) {
                        var result = JSON.parse(data.Data);
                        $("#AddTime").text(result.P_Name);
                        $("#BeginTime").text(result.D_Name);
                        $("#Originator").text(result.Name);
                        $("#Participant").text(result.AddTime);
                        $("#PendTime").text(result.AddTime);
                        $("#Remark").text(result.Reason);
                        $("#State").text(Aid);
                        $("#TaskTypeName").text(result.Type);
                        //多窗口模式，层叠置顶
                        layer.open({
                            id: 1,
                            type: 1,
                            shift: 2,
                            shadeClose: false,
                            title: "任务详情",
                            skin: "layui-layer-rim",
                            area: ["50%"],
                            maxmin: true,
                            closeBtn: 1,
                            content: $("#test").html()

                            //end:function(){

                            //    location

                        });
                        /* 渲染表单 */
                        form.render();

                    }
                });
            },
            loginSubmit: function (data) {
                $.ajax({
                    type: "POST",
                    url: "/TransferBoss/TransferGet",//发送请求  
                    data: data.field,
                    dataType: "json",
                    success: function (result) {
                        if (result.State == 1) {
                            layer.alert(result.Message, { icon: 7 });

                        }
                        else
                            layer.msg(result.Message, { icon: 1 }, function (index) {
                                //location.href = '/Page/Index';
                                layer.close(index);
                            });
                    }
                });
            }
        }


        $("body").on("click", ".news_edit", function () {
            getList(pageData);
            var _this = $(this).parent().parent();
            Aid = _this.children('td').eq(0).text();
            active.setTop(Aid);
        });



        $("#validateCode").click(function () {
            loginManager.loginSubmitloginSubmit();
        });


        //监听提交
        form.on('submit(TransferAdd)', function (data) {
            active.loginSubmit(data);
            layer.closeAll();
            getList(pageData);
            return false;
        });

        //渲染数据
        function renderDate(list) {

            var dataHtml = '';
            if (list != null) {
                layui.each(list, function (index, item) {

                    if (item.Sex == true) { item.Sex = "男"; }
                    else { item.Sex = "女"; }
                    dataHtml += '<tr>'
                    + '<td>' + item.Id + '</td>'
                    + '<td>' + item.Name + '</td>'
                    + '<td>' + item.Sex + '</td>'
                    + '<td>' + item.P_Name + '</td>'
                    + '<td>' + item.D_Name + '</td>'
                    + '<td>' + item.AddTime + '</td>'
                    + '<td>' + item.Reason + '</td>'
                    //+ '<td>' + item.Type + '</td>'
                    + '<td>'
                    + '<a id="postId" class="layui-btn layui-btn-mini news_edit" data-id="' + item.Id + '"><i class="iconfont icon-edit"></i> 审批</a>'
                    + '</td>'
                    + '</tr>';
                });
            } else {
                dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
            }
            $(".news_content").html(dataHtml);   //渲染数据
            form.render(); //渲染表单
        }

    }



    //分页
    function showPage(page) {
        laypage.render({
            elem: 'page',
            count: page.RowCount,//得到总页数
            limit: page.PageSize,
            curr: page.PageNumber + 1,
            groups: 5, //连续显示分页数
            jump: function (obj, first) {
                if (!first) {//防止无限刷新
                    page.PageNumber = obj.curr - 1;
                    getList(page);
                }
            }
        });
    }

    function Select() {
        var url = "/TransferBoss/Select";
        $.post(url, function (result) {
            var departments = JSON.parse(result.data);
            var deptSelect = $("#deptSelect");
            layui.each(departments, function (index, data) {
                var deptOption = $("<option value='" + data.D_Name + "'>" + data.D_Name + "</option>");
                deptSelect.append(deptOption);
            });
            form.render();
        });
    };


    $(function () {
        Select();
        getList(pageData);
        $('.news_read').click(function () {
            pageData.D_NameR = $("select option:selected").attr("value");
            pageData.TypeR = $('#TypeR input[name="Type"]:checked ').val();
            getList(pageData);
        });
        //$(document).on("click", ".news_edit", function () {

        //    var _this = $(this).parent().parent();
        //    Aid = _this.children('td').eq(0).text();
        //    active.setTop(Aid);
        //});
        //$('.edit').click(function () {
        //    data.Id = $("#State").html();
        //    data.TypeS = $('#TypeS input[name="TypeS"]:checked ').val();;
        //    data.res = $('.res').val();
        //    active.loginSubmit(data);
        //})
    });
})